#ifndef PARAMETERS_H_
#define PARAMETERS_H_

#include "main.h"
#include "RTC_calendar.h"
#include "modbus.h"
#include "ip_addr.h"

#define MODBUS_MAX_REQUEST				8
#define MODBUS_MAX_REQUEST_BODY			30
#define MODBUS_MAX_REQUEST_CAPTION		6
#define MODBUS_CRC_LENG					2

#define ADDRESS_PARAM_GENERAL	0

#define FINAL_OK	0x55

/* Flash memory general descriptor 200 */
typedef struct sParametersGeneral
{
	/* System parameters */
	uint32_t id_plk;
	uint8_t poll_interval[3];
	uint8_t modbus_baudrate[4];
	uint16_t modbus_id_line[4];
	/* Network parameters */
	uint8_t network_enabled;
	uint8_t network_priority;
	/* Ethernet */
	uint8_t mac[6];
	uint8_t ip_addr_default[4];
	uint8_t netmask_default[4];
	uint8_t gateway_default[4];
	uint8_t dest_ip[4];
	uint16_t dest_port;
	/* GSM */
	char gsm_apn[64];
	uint8_t reserv[95];
	uint8_t final;
}sParamGeneral;


/* Flash memory secondary descriptor 200 */
#define MODBUS_MAX_MARCKERS 30
typedef struct sMarker
{
	uint8_t replyMarckersAll;
	uint8_t marckerStart[MODBUS_MAX_MARCKERS];
	uint8_t marckerLeng[MODBUS_MAX_MARCKERS];
}sMarker;

typedef struct sParametersSecondary
{
	uint8_t typeRequest;/* standard, raw */
	uint8_t leng_id;     /* 1-255 bytes */
	uint8_t channel;		/* used channel */
	uint8_t reqData[MODBUS_MAX_REQUEST_BODY];
	uint8_t reqLeng;
	char deviceCaption[MODBUS_MAX_REQUEST_CAPTION];
	char reqCaption[MODBUS_MAX_REQUEST_CAPTION];
	char comment[50];
	uint16_t category;
	sMarker markers;
	uint8_t final;
}sParamSecondary;

#define E_NETWORK_USE_ETHERNET          	0x01
#define E_NETWORK_USE_GSM              		0x02
#define E_NETWORK_USE_PRIORITY_ETHERNET		0x04
#define E_NETWORK_USE_PRIORITY_GSM      	0x08
#define E_NETWORK_USE_DHCP              	0x10



typedef struct sStartNetParam
{
	uint8_t ip_addr[4];
	uint8_t gw_addr[4];
	uint8_t sub[4];
	uint8_t mac[6];
	uint8_t net_use;
	uint8_t ip_dest[4];
	uint16_t destPort;
}sStartNetParam;

typedef struct sStartParam
{
	sStartNetParam net;
	uint16_t id;
	uint8_t mode;
	uint8_t poll_interval[3];
	uint16_t modbus_id_lines[4];
}sStartParam;

extern sStartParam startParam;

#define FLASH_MISO_PIN				GPIO_PIN_10
#define FLASH_MISO_PORT				GPIOC
#define FLASH_MOSI_PIN				GPIO_PIN_3
#define FLASH_MOSI_PORT				GPIOD
#define FLASH_SCLK_PIN				GPIO_PIN_2
#define FLASH_SCLK_PORT				GPIOD
/* M25P64 */
#define FLASH_M25P64_RCC_AHB		AHB1ENR
#define	FLASH_M25P64_RCC_AHB1_EN	( RCC_AHB1ENR_GPIOCEN | RCC_AHB1ENR_GPIODEN )
#define FLASH_M25P64_CS_PIN			GPIO_PIN_12
#define FLASH_M25P64_CS_PORT		GPIOC
#define FLASH_M25P64_WP_PIN			GPIO_PIN_11
#define FLASH_M25P64_WP_PORT		GPIOC

#define _FLASH_CMD_WRITE			0x02  /*!< Write to Memory instruction */
#define _FLASH_CMD_WRSR				0x01  /*!< Write Status Register instruction */
#define _FLASH_CMD_WREN				0x06  /*!< Write enable instruction */
#define _FLASH_CMD_WRDIS			0x04  /*!< Write disabled instruction */
#define _FLASH_CMD_READ				0x03  /*!< Read from Memory instruction */
#define _FLASH_CMD_RDSR				0x05  /*!< Read Status Register instruction  */
#define _FLASH_CMD_RDID				0x9F  /*!< Read identification */
#define _FLASH_CMD_SE				0xD8  /*!< Sector Erase instruction */
#define _FLASH_CMD_BE				0xC7  /*!< Bulk Erase instruction */

#define _FLASH_WIP_FLAG				0x01  /*!< Write In Progress (WIP) flag */

#define _FLASH_DUMMY_BYTE			0xA5
#define _FLASH_SPI_PAGESIZE			0x100

#define _FLASH_M25P128_ID			0x202018
#define _FLASH_M25P64_ID			0x202017

#define FLASH_M25P64_WRITE_EN		0x00
#define FLASH_M25P64_WRITE_DIS		0x01

#define FLASH_MOSI_0 		FLASH_MOSI_PORT->ODR &=~ FLASH_MOSI_PIN
#define FLASH_MOSI_1 		FLASH_MOSI_PORT->ODR |= FLASH_MOSI_PIN
#define FLASH_SCLK_0 		FLASH_SCLK_PORT->ODR &=~ FLASH_SCLK_PIN
#define FLASH_SCLK_1 		FLASH_SCLK_PORT->ODR |= FLASH_SCLK_PIN
#define FLASH_M25P64_CS_0 	FLASH_M25P64_CS_PORT->ODR &=~ FLASH_M25P64_CS_PIN
#define FLASH_M25P64_CS_1 	FLASH_M25P64_CS_PORT->ODR |= FLASH_M25P64_CS_PIN
#define FLASH_M25P64_WP_0 	FLASH_M25P64_WP_PORT->ODR &=~ FLASH_M25P64_WP_PIN
#define FLASH_M25P64_WP_1 	FLASH_M25P64_WP_PORT->ODR |= FLASH_M25P64_WP_PIN
#define FLASH_MISO_STATE	( FLASH_MISO_PORT->IDR & FLASH_MISO_PIN )

void ParamInit(void);

void Flash_Mem_Init(void);
void Flash_M25P64_EraseSector(uint8_t addrSector);
void Flash_M25P64_Read_Buffer(uint8_t* pBuffer, uint32_t addr, uint16_t numByteToRead);
void FLASH_M25P64_Write_Buffer(uint8_t* pBuffer, uint32_t writeAddr, uint16_t len);
void FLASH_M25P64_Write_Page(uint8_t* pBuffer, uint32_t writeAddr, uint16_t numByteToWrite);
void Flash_M25P64_EraseSector(uint8_t addrSector);
uint32_t Flash_M25P64_Read_ID(void);
void Flash_M25P64_EraseAll(void);

#endif
